layui.use(['form','treeSelect','layer','upload','laydate','layedit','tableSelect'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var treeSelect = layui.treeSelect;

    //-----------
    var newsDetail;
    layui.use('layedit', function(){
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/upload/editPicUpload' //接口url
                ,type: 'post' //默认post
            }
        });
        newsDetail=layedit.build('newsDetail',{
            height: 160 //设置编辑器高度
        }); //建立编辑器
    });

    //-----------
    //---------------------------------下拉搜索框
    var tableSelect = layui.tableSelect;
    tableSelect.render({
        elem: '#produceName',	//定义输入框input对象
        checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'produceName',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '关键词搜索',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url:'/agriecomProduce/list',
            cols: [[
                { type: "radio", fixed:"left", width:50},
                { field: 'produceId', title: '商品ID' },
                { field: 'produceName', title: '商品名称' },
                { field: 'price', title: '价格' }
            ]]
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            $("#produceId").val(data.data[0].produceId);
            $("#produceName").val(data.data[0].produceName);
            $("#price").val(data.data[0].price);
            //只要修改产品就修改趋势
            $("#trend").val(data.data[0].price);
        }
    })
    //---------------------------------


    //回显数据
    var request = GetRequest();
     $.get("/quotation/" + request.id,function (data) {
        $("#id").val(data.id);
        $("#name").val(data.name);
        $("#recordItem").val(data.recordItem);
        $("#produceId").val(data.produceId);
        $("#trend").val(data.trend);
        $.get("/agriecomProduce/" + data.produceId,function (produce) {
            $("#produceName").val(produce.data.produce.produceName);
            $("#price").val(produce.data.produce.price);
         });
    });


    form.on("submit(edit)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.post("/quotation/edit",{
            id : $("#id").val(),
            name: $("#name").val(),
            recordItem:$("#recordItem").val(),
            produceId: $("#produceId").val(),
            trend: $("#trend").val()
        },function(res){
            //
        })
        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("修改成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);
        return false;
    })
    //修改场景信息

    var upload = layui.upload;
    //上传头像

    upload.render({
        elem: '.userFaceBtn',
        field: 'uploadFile',//上传文件的名称，要和接口参数名称相同
        url: '/upload/pic',
        //multiple:true,
        done: function(res, index, upload){
            if (res.code === 0) {
                layer.msg(res.msg);
                $("#uploadImage").attr("src",res.url);
                $("#photo").val(res.url);
            } else {
                layer.msg(res.msg);
            }
        }
    });
    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});